package mcu.market.swing;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.WindowConstants;

import mcu.market.set.XmlSet;

/**
 * 
 * @author angle
 * 應用程式工具
 * 2010/3/10
 */
public class AppTool {
	JFrame ob;
	String s_path="..\\set\\data.xml";
	String img_path="\\image\\icon.jpg";
	XmlSet xs= XmlSet.getInstance();
	public AppTool(JFrame jf){
		ob=jf;
		ActionListener openListener = new ActionListener(){
        	@Override
			public void actionPerformed(ActionEvent arg0) {
        		ob.setVisible(true);
        	}
        };
        ActionListener exitListener = new ActionListener() {
        	@Override
			public void actionPerformed(ActionEvent e) {
        	xs.SetValue("/project/open", "false");
        	JOptionPane.showMessageDialog(null, "關閉程式");
        	System.exit(0);
        	}
        };
		SystemTray tray = SystemTray.getSystemTray();
		Image image = Toolkit.getDefaultToolkit()
                          .getImage(AppTool.class.getResource("image/icon.jpg"));
		jf.setIconImage(image);
		jf.setTitle(xs.getSetValue("/project/title"));
		PopupMenu popup = new PopupMenu();
		MenuItem item = new MenuItem("open");
		MenuItem exititem = new MenuItem("exit");
		popup.add(item);
		popup.addSeparator();
		popup.add(exititem);
		item.addActionListener(openListener);
		exititem.addActionListener(exitListener);
		TrayIcon trayIcon = 
			new TrayIcon(image, xs.getSetValue("/project/title"), popup);
		trayIcon.addMouseListener(addMouseEvent());
		this.ob.addWindowListener(addWindowEvent(this.ob));
		this.ob.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		try {
			tray.add(trayIcon);
		} catch (AWTException e) {
			System.err.println("無法加入系統工具列圖示");
			e.printStackTrace();
		}
		AppChecker();
	}
	private MouseListener addMouseEvent() {
		return new MouseListener(){
			
		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			ob.setVisible(true);
		}
			
		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
			
		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}
			
		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}};
	}
	private WindowListener addWindowEvent(JFrame jf) {
		return new WindowListener() {
			
			@Override
			public void windowOpened(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowIconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeiconified(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowDeactivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				ob.setVisible(false);
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void windowActivated(WindowEvent e) {
				// TODO Auto-generated method stub
				
			}
		};
		
	}
	private void AppChecker() {
		
		boolean bl= Boolean.parseBoolean(xs.getSetValue("/project/open"));
		System.out.print(XmlSet.class.getResource("DataConfig.xml").toString());
		if(!bl){
			XmlSet.getInstance().SetValue("/project/open", "true");
		}else{
			JOptionPane.showMessageDialog(null, "已開啟程式,請檢查常駐列!!");
			System.exit(0);
		}
	}
}
